package web

import (
	"github.com/aphasia51/website/internal/domain"
	"github.com/aphasia51/website/internal/service"
	"github.com/gin-gonic/gin"
	"net/http"
)

type ExperimentalPersonnelHandler struct {
	svc *service.ExperimentalPersonnelService
}

func NewExperimentalPersonnelHandler(svc *service.ExperimentalPersonnelService) *ExperimentalPersonnelHandler {
	return &ExperimentalPersonnelHandler{
		svc: svc,
	}
}

func (a *ExperimentalPersonnelHandler) RegistryRoutes(server *gin.Engine) {
	ag := server.Group("/experimental-personnel")
	ag.POST("/query", a.Query)
	ag.POST("/query-certificate", a.QueryCertificate)
}

func (a *ExperimentalPersonnelHandler) Query(ctx *gin.Context) {
	type QueryReq struct {
		Name string `json:"name"`
	}
	var req QueryReq
	if err := ctx.Bind(&req); err != nil {
		panic(err)
	}
	Equipment, err := a.svc.Query(ctx, domain.ExperimentalPersonnel{
		Name: req.Name,
	})

	if err != nil {
		ctx.String(http.StatusOK, "query error")
		return
	}

	ctx.JSON(http.StatusOK, Equipment)
}

func (a *ExperimentalPersonnelHandler) QueryCertificate(ctx *gin.Context) {
	type QueryReq struct {
		PeopleId string `json:"people_id"`
	}
	var req QueryReq
	if err := ctx.Bind(&req); err != nil {
		panic(err)
	}
	certificateInfo, err := a.svc.QueryCertificate(ctx, req.PeopleId)

	if err != nil {
		ctx.String(http.StatusOK, "query error")
		return
	}

	ctx.JSON(http.StatusOK, certificateInfo)
}
